package com.foxconn.ipebg.common.system.controller;

import ipebg.mzk.facade.entity.MsAdminRole;
import ipebg.mzk.facade.entity.MsAdminUserRole;
import ipebg.mzk.facade.system.service.RoleService;
import ipebg.mzk.facade.system.service.UserRoleService;
import ipebg.mzk.facade.system.service.UserService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;


/**
 * 對菜單項操作方法
 * @author F3225975
 *
 */
@Controller
@RequestMapping("/userrole.do")
public class UserRoleController {
	 
	 @Resource(name="userServiceImpl")
	 UserService userService;
	 
	 @Resource(name="roleServiceImpl")
	 RoleService roleService;
	 
	 @Resource(name="userRoleServiceImpl")
	 UserRoleService userRoleService;
	 
	/**
	 * 加載用戶列表頁面方法
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "action=load")
	public String load(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		String[] orderBy = new String[]{"roleName asc"};
		List<MsAdminRole> roleList = roleService.getAllRoleList(orderBy);
		model.addAttribute("roleList", roleList);
		return "/pages/common/system/user_role_manage";
	}
				
	/**
	 * 获取某个用户具有的RoleList列表
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(params = "action=getRoles")
	public void getRoles(HttpServletRequest request, HttpServletResponse response, Model model) {
		String userId = request.getParameter("userId");
		List<MsAdminRole> roleList = userRoleService.getRoleListByUserId(userId);
		List  lists = new ArrayList();
		for(int i=0;i<roleList.size();i++){
			Map m = new HashMap();
			MsAdminRole role = roleList.get(i);
			m.put("roleId", role.getRoleId());
			m.put("roleName", role.getRoleName());
			lists.add(m);
		}
		try {
			response.getWriter().write(JSON.toJSONString(lists));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 保存用戶RoleList
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "action=saveRoles")
	public void saveRoles(HttpServletRequest request, HttpServletResponse response, Model model) {
		String userId = request.getParameter("userId");
		/**
		 * 獲取所有的用戶角色列表，并刪除之。
		 */
		List<MsAdminUserRole> userRoleList = userRoleService.getUserRoleListByUserId(userId);
		for(int i = 0;i<userRoleList.size();i++){
			userRoleService.delete(userRoleList.get(i));
		}
		
		String role =  request.getParameter("roles");
		if(role == "" || role.equals("")){
			
		}else{
			role = role.substring(0, role.length()-1);
			String[] roles = role.split(",");
			
			/**
			 * 添加從前台獲取的所有的角色，并保存之
			 */
			for(int j = 0;j<roles.length;j++){
				MsAdminUserRole userRole = new MsAdminUserRole();
				userRole.setAdminId(userId);
				userRole.setRoleId(roles[j]);
				userRoleService.save(userRole);
			}
		}
		try {
			response.getWriter().write("success");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
